/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.core.auth.util;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

/**
 * Created by walle on 12/9/15.
 */
public class Urls {
    private boolean checkpopedom(Map<String, Object> actionMap, Map<String, Map<String, String>> useraction, String url) {
        if (actionMap == null) {
            return false;
        }
        if (actionMap.get(url) != null) {
            if (useraction == null) {
                return false;
            }
            if (useraction.get(url) != null) {
                return true;
            }
            return false;
        }

        return true;
    }

    public static String formatUrl(String requestUrl, String basePath) {
        requestUrl = requestUrl.replace(basePath, "");

        int num = requestUrl.indexOf("?");
        if (num > 0) {
            requestUrl = requestUrl.substring(0, num);
        }

        return requestUrl;
    }

    public static boolean isActionByUrl(String _Url, String _postfix) {
        if (_Url.indexOf(".") < 0) {
            return false;
        }
        _Url = _Url.trim();
        String postfix = _Url.substring(_Url.lastIndexOf(".") + 1);
        if (postfix.toUpperCase().equals(_postfix.toUpperCase())) {
            return true;
        }
        return false;
    }

    public static String getActionKey(String formatUrl) {
        String name = formatUrl.substring(0, formatUrl.lastIndexOf("."));

        return name;
    }

    public static String getActionSubKey(String actionkey) {
        int num2 = actionkey.replace("\\", "/").lastIndexOf("/");
        if (num2 >= 0) {
            actionkey = actionkey.substring(num2);
        }
        return actionkey;
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String getUrlParameters(HttpServletRequest request) {
        Set<Map.Entry> entrySet = request.getParameterMap().entrySet();
        StringBuffer urlp = new StringBuffer();
        for (Map.Entry node : entrySet) {
            if (((node.getValue() != null ? 1 : 0) & (((String[]) node.getValue()).length > 0 ? 1 : 0)) != 0) {
                if (urlp.length() > 0) {
                    urlp.append("&");
                    urlp.append((String) node.getKey() + "=" + ((String[]) node.getValue())[0]);
                } else {
                    urlp.append((String) node.getKey() + "=" + ((String[]) node.getValue())[0]);
                }
            }
        }
        return urlp.toString();
    }

    public boolean isHavePop(String URL_index, Map<String, String> userUrl) {
        return false;
    }
}
